#include "stdafx.h"
#include "Game.h"

#include "MainMenu.h"
#include "SplashScreen.h"
#include "PlayerPaddle.h"
#include "GameBall.h"
#include "ServiceLocator.h"
#include "SFMLSoundProvider.h"
#include "AIPaddle.h"
#include "InputManager.h"
#include "SFMLScoreProvider.h"

void Game::Start(void)
{
	if (_gameState != Unitialized)
		return;

	_mainWindow.create(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32),"Pang!");
	//_mainWindow.SetFramerateLimit(60);

	SFMLSoundProvider soundProvider;
	ServiceLocator::RegisterAudio(&soundProvider);

	SFMLScoreProvider scoreProvider(_mainWindow);
	ServiceLocator::RegisterScore(&scoreProvider);

	ServiceLocator::GetAudio()->PlaySong("audio/Soundtrack.ogg",true);
	
	ServiceLocator::RegisterInput(&_inputManager);

	PlayerPaddle *player1 = new PlayerPaddle();
	player1->SetPosition((SCREEN_WIDTH / 2), SCREEN_HEIGHT - 40);

	AIPaddle *player2 = new AIPaddle();
	player2->SetPosition((SCREEN_WIDTH / 2), 40);

	GameBall *ball = new GameBall();
	ball->SetPosition((SCREEN_WIDTH / 2), SCREEN_HEIGHT / 2 + 40);

	_gameObjectCache.Add("Paddle1", player1);
	_gameObjectCache.Add("Ball", ball);
	_gameObjectCache.Add("Paddle2",player2);

	_gameState = Game::ShowingSplash;

	while (!IsExiting())
	{
		GameLoop();
	}

	_mainWindow.close();
}

bool Game::IsExiting()
{
	if (_gameState == Game::Exiting)
		return true;
	else
		return false;
}

sf::RenderWindow& Game::GetWindow()
{
	return _mainWindow;
}

const bool Game::GetInput(sf::Keyboard::Key key)
{
	return sf::Keyboard::isKeyPressed(key);
}

void Game::GameLoop()
{
	sf::Event currentEvent;
	_mainWindow.pollEvent(currentEvent);
	switch(_gameState)
	{
	case::Game::ShowingMenu:
		ShowMenu();
		break;
	case::Game::ShowingSplash:
		ShowSplashScreen();
		break;
	case Game::Playing:

		_inputManager.Update(); //Refresh input positions
		_gameObjectCache.UpdateAll(); //Update all objects 
		_gameObjectCache.CheckCollisions();

		_mainWindow.clear(sf::Color(0, 0, 0));
		_gameObjectCache.DrawAll(_mainWindow);
		_mainWindow.display();


		if (currentEvent.type == sf::Event::Closed)
		{
			_gameState = Game::Exiting;
		}
		if (currentEvent.type == sf::Event::KeyPressed)
		{
			switch(currentEvent.key.code)
				case sf::Keyboard::Escape:
					ShowMenu();
					break;
		}
		break;
		
	}
}

void Game::ShowSplashScreen()
{
	SplashScreen splashScreen;
	splashScreen.Show(_mainWindow);
	_gameState = Game::ShowingMenu;
}

void Game::ShowMenu()
{
	_gameObjectCache.Pause(true);
	MainMenu mainMenu;
	MainMenu::MenuResult result = mainMenu.Show(_mainWindow);
	switch (result)
	{
	case MainMenu::Exit:
		_gameState = Game::Exiting;
		break;
	case MainMenu::Play:
		_gameState = Game::Playing;
		_gameObjectCache.Pause(false);
		break;
	}
}

const GameObjectCache& Game::GetGameObjectCache()
{
	return _gameObjectCache;
}


Game::GameState Game::_gameState = Game::Unitialized;
sf::RenderWindow Game::_mainWindow;
GameObjectCache Game::_gameObjectCache;
InputManager Game::_inputManager;